package com.duanyu.mysecurity.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;

public class MyLoginSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //拿到用户登录信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();

        System.out.println("用户名:"+userDetails.getUsername());
        System.out.println("一些操作");

        //重定向到主页面
        response.sendRedirect("/main.html");

    }
}
